home *** CD-ROM | disk | FTP | other *** search
- Path: dialup-86.austin.io.com!user
- From: hamilton@shokwave.com (Jim Hamilton)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: Wed, 24 Jan 1996 01:04:02 -0600
- Organization: Disorganized
- Message-ID: <hamilton-2401960104020001@dialup-86.austin.io.com>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com> <4dpcfo$293@clarknet.clark.net>
- NNTP-Posting-Host: dialup-86.austin.io.com
- X-Newsreader: Yet Another NewsWatcher 2.0.5b5
-
- In article <4dpcfo$293@clarknet.clark.net>, gusty@clark.net (Harlan
- Messinger) wrote:
-
- >Jim Hamilton (hamilton@shokwave.com) wrote:
- >: In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
- >: <100336.3326@CompuServe.COM> wrote:
- >:
- >: >Consider
- >: >#include <iostream>
- >: >int main() {
- >: > int i=1;int j=i+(i+=1);
- >: > cout<<i<<','<<j<<'\n';
- >: > return 0;
- >: >}
- >: >on my compiler this produces 2,4
- [deleted]
-
- >: The highest precedence in any expression is the insides of parentheses
- >: (). Therefore (i+=1) is evaluated before i+().
- >
- >True, but that's not actually the problem here. The problem is whether
- >(i+=1) gets evaluated before the i to the left of the plus sign.
-
- And I said, what's inside the parentheses (i+=1) *does* gets evaluated
- before the i to the left of the plus sign.
-
- Anyway, according to K&R, order of evaluation of operands of a commutative
- operator is undefined by the language. If it were legal to write:
- j = i + i+=1; // spacing for readability of what's intended
- then the value of j would be undefined (either 3 or 4).
-
- Of course, that statement is not legal, as operator precedence makes it
- look more like:
- j = (i + i) += 1; // error: (i + i) is not an lvalue
-
- --
- JFH
-
- [This .sig intentionally left blank.]
-